import { initParticlesJS } from '../particles.js'

// 页面加载完成后初始化粒子效果
        document.addEventListener('DOMContentLoaded', function() {
            // 检查用户登录状态并更新界面
            updateUserInterface();

            //粒子效果
            initParticlesJS();

            // 显示最新帖子
            displayLatestPosts();

            // 处理正在开发中的功能
            setupDevelopmentFeatures();

            // 移动菜单切换
            document.getElementById('mobile-menu-btn').addEventListener('click', function() {
                const mobileMenu = document.getElementById('mobile-menu');
                if (mobileMenu.classList.contains('hidden')) {
                    mobileMenu.classList.remove('hidden');
                } else {
                    mobileMenu.classList.add('hidden');
                }
            });

            // 添加购物车功能
            setupCartFunctions();

            // 初始化轮播图
            initBannerSlider();
        });

        // 更新用户界面
        function updateUserInterface() {
            const user = localStorage.getItem('currentUser') ? JSON.parse(localStorage.getItem('currentUser')) : null;
            const authSection = document.getElementById('user-auth-section');
            const infoSection = document.getElementById('user-info-section');
            
            if (user) {
                authSection.classList.add('hidden');
                infoSection.classList.remove('hidden');
                document.getElementById('username').textContent = user.username;

                // 退出登录
                document.getElementById('logout-btn').addEventListener('click', function() {
                    localStorage.removeItem('currentUser');
                    window.location.reload();
                });
            } else {
                authSection.classList.remove('hidden');
                infoSection.classList.add('hidden');
            }

            // 更新购物车数量
            updateCartCount();
        }

        // 更新购物车数量
        function updateCartCount() {
            const cart = localStorage.getItem('cart') ? JSON.parse(localStorage.getItem('cart')) : [];
            const cartCount = document.getElementById('cart-count');
            cartCount.textContent = cart.length;
        }

        // 显示最新帖子
        function displayLatestPosts() {
            const postsContainer = document.getElementById('latest-posts');
            
            // 模拟帖子数据，实际应用中应从服务器获取
            const posts = [
                {
                    id: 1,
                    title: "分享：昨天在太湖钓获的大鲫鱼",
                    author: "水中游龙",
                    date: "2025-05-01",
                    likes: 24,
                    comments: 8,
                    category: "淡水钓鱼"
                },
                {
                    id: 2,
                    title: "求推荐：适合初学者的海钓装备",
                    author: "新手钓手",
                    date: "2025-05-02",
                    likes: 15,
                    comments: 12,
                    category: "装备讨论"
                },
                {
                    id: 3,
                    title: "技巧分享：如何钓好白条",
                    author: "钓鱼老教授",
                    date: "2025-05-03",
                    likes: 36,
                    comments: 17,
                    category: "钓技研究"
                },
                {
                    id: 4,
                    title: "活动通知：本周日江边垂钓聚会",
                    author: "活动组织者",
                    date: "2025-05-04",
                    likes: 29,
                    comments: 22,
                    category: "社区活动"
                }
            ];

            // 清空容器
            postsContainer.innerHTML = '';

            // 添加帖子
            posts.forEach(post => {
                const postElement = document.createElement('div');
                postElement.className = 'border-b border-gray-200 pb-4';
                postElement.innerHTML = `
                    <div class="flex justify-between items-start">
                        <div>
                            <h3 class="font-bold text-lg mb-1">
                                <a href="post.html?id=${post.id}" class="hover:text-blue-600 transition">${post.title}</a>
                            </h3>
                            <p class="text-sm text-gray-500">
                                <span class="bg-blue-100 text-blue-800 text-xs px-2 py-1 rounded mr-2">${post.category}</span>
                                <span class="mr-2"><i class="fas fa-user mr-1"></i>${post.author}</span>
                                <span><i class="far fa-calendar mr-1"></i>${post.date}</span>
                            </p>
                        </div>
                        <div class="text-sm text-gray-500">
                            <span class="mr-3"><i class="far fa-thumbs-up mr-1"></i>${post.likes}</span>
                            <span><i class="far fa-comment mr-1"></i>${post.comments}</span>
                        </div>
                    </div>
                `;
                postsContainer.appendChild(postElement);
            });
        }

        // 设置购物车功能（修改部分）
        function setupCartFunctions() {
            const addToCartButtons = document.querySelectorAll('.add-to-cart');
            
            addToCartButtons.forEach(button => {
                button.addEventListener('click', function() {
                    // 新增：登录状态检查
                    if (!localStorage.getItem('currentUser')) {
                        showNotification('请先登录后再添加商品到购物车');
                        setTimeout(() => window.location.href = 'auth.html', 2000);
                        return;
                    }
                    
                    const id = this.getAttribute('data-id');
                    const name = this.getAttribute('data-name');
                    const price = this.getAttribute('data-price');
                    const img = this.getAttribute('data-img');
                    
                    let cart = localStorage.getItem('cart') ? JSON.parse(localStorage.getItem('cart')) : [];
                    cart.push({ id, name, price, img, quantity: 1 });
                    localStorage.setItem('cart', JSON.stringify(cart));
                    updateCartCount();
                    showNotification('已添加商品到购物车');
                });
            });
        }

        // 显示通知
        function showNotification(message) {
            const notification = document.getElementById('notification');
            notification.textContent = message;
            notification.classList.remove('hidden');
            notification.classList.remove('translate-y-20', 'opacity-0');
            
            setTimeout(() => {
                notification.classList.add('translate-y-20', 'opacity-0');
                setTimeout(() => {
                    notification.classList.add('hidden');
                }, 300);
            }, 2000);
        }

        // 设置正在开发中的功能
        function setupDevelopmentFeatures() {
            const devFeatureButtons = [
                document.getElementById('communityBtn'),
                document.getElementById('techniqueBtn'),
                document.getElementById('fishingMapBtn'),
                document.getElementById('eventsBtn')
            ];
            
            const devModal = document.getElementById('development-modal');
            const closeDevModal = document.getElementById('close-dev-modal');
            
            devFeatureButtons.forEach(button => {
                if (button) {
                    button.addEventListener('click', function(e) {
                        e.preventDefault();
                        devModal.classList.remove('hidden');
                    });
                }
            });
            
            closeDevModal.addEventListener('click', function() {
                devModal.classList.add('hidden');
            });
        }

        // 初始化轮播图
        function initBannerSlider() {
            const sliderBg = document.getElementById('slider-bg');
            const images = [
                '/assets/bgs/bg1.png',
                '/assets/bgs/bg2.jpeg',
                '/assets/bgs/bg.jpeg'
            ];
            let currentIndex = 0;
        
            function changeImage() {
                currentIndex = (currentIndex + 1) % images.length;
                sliderBg.style.backgroundImage = `url(${images[currentIndex]})`;
                sliderBg.style.opacity = 0;
                setTimeout(() => {
                    sliderBg.style.opacity = 1;
                }, 50);
            }
        
            // 初始设置第一张图
            sliderBg.style.backgroundImage = `url(${images[0]})`;
            sliderBg.style.opacity = 1;
            
            // 每5秒切换一次
            setInterval(changeImage, 5000);
        }